package activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.studyAndroid.R;
import utils.Config;

public class LoginActivity extends Activity implements View.OnClickListener {

    private EditText name;
    private EditText pass;
    private Button button;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        //设置全屏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.login);
        init();
    }

    public void init() {
        name = (EditText) findViewById(R.id.name);
        name.setText(MyApplication.getInstance().sharedPreferences.getString(Config.USER_NAME, ""));
        pass = (EditText) findViewById(R.id.pass);
        pass.setText(MyApplication.getInstance().sharedPreferences.getString(Config.PASS_WORD, ""));
        button = (Button) findViewById(R.id.loginButton);
        button.setOnClickListener(this);
    }

    public void login() {
        String na = name.getText().toString().trim();
        if (!(na.equals("沙晓杰") || na.equals("谢叶雷"))) {
            Toast.makeText(this, "用户名不存在！", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!pass.getText().toString().trim().equals("1234")) {
            Toast.makeText(this, "密码错误！", Toast.LENGTH_SHORT).show();
            return;
        }
        MyApplication.getInstance().edit.putString(Config.USER_NAME, name.getText().toString().trim());
        MyApplication.getInstance().edit.putString(Config.PASS_WORD, pass.getText().toString().trim());
        MyApplication.getInstance().edit.commit();
        Intent intent = new Intent(LoginActivity.this, GridViewActivity.class);
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loginButton:
                login();
                break;
        }

    }
}
